<template>
  <div>
    <!-- 添加职位 -->
    <div>
      <el-input
          size="small"
          prefix-icon="el-icon-plus"
          style="width: 300px"
          v-model="jl.name"
          placeholder="添加职称等级....."
      ></el-input>
      <el-select size="small" style="margin-left: 6px" v-model="jl.titleLevel" placeholder="职称等级">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button @click="addJoblevel" style="margin-left: 6px" type="primary" size="small" icon="el-icon-plus">添加</el-button>
    </div>
    <!-- 表格展示数据 -->
    <div style="margin-top: 10px">
      <el-table
          :data="jls"
          stripe
          border
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 70%">
        <!-- 多选框 -->
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名"
            width="150">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称等级"
            width="150">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间"
            width="150">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button
                @click="showDialog(scope.row)"
                size="small">编辑</el-button>
            <el-button
                size="small"
                @click="deleteJoblevel(scope.row)"
                type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button
          size="small"
          style="margin-top: 10px"
          :disabled="this.multipleSelection.length == 0"
          @click="deleteMany"
          type="danger">批量删除</el-button>
    </div>
    <!-- 弹出框 -->
    <el-dialog
        title="编辑"
        :visible.sync="dialogVisible"
        width="30%">
      <table>
        <tr>
          <td>
            <el-tag>职称名称</el-tag>
          </td>
          <td>
            <el-input
                style="margin-left: 6px"
                size="small"
                v-model="updateJl.name"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职称等级</el-tag>
          </td>
          <td>
            <el-select size="small" style="margin-left: 6px" v-model="updateJl.titleLevel" placeholder="职称等级">
              <el-option
                  v-for="item in titleLevels"
                  :key="item"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>是否启用</td>
          <td>
            <el-switch
                style="margin-left: 6px"
                v-model="updateJl.enabled"
                active-color="#13ce66"
                inactive-color="#f00"
                active-text="已启用"
                inactive-text="未启用">
            </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button
            size="small"
            @click="dialogVisible = false">取 消</el-button>
        <el-button
            size="small"
            type="primary"
            @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data(){
    return {
      jl:{
        name: '',
        titleLevel:'',
      },
      updateJl: {
        name: '',
        titleLevel: '',
        enabled: false,
      },
      titleLevels: [
          '正高级',
          '副高级',
          '中级',
          '初级',
          '员级'
      ],
      jls: [],
      dialogVisible: false,
      multipleSelection : [],
    };
  },
  mounted() {
    this.initJls();
  },
  methods:{
    // 批量删除
    deleteMany(){
      this.$confirm(`此操作将永久删除[${this.multipleSelection.length}]条职称，是否继续?`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(()=>{
        // 点击了确定
        let ids = '?';
        this.multipleSelection.forEach((item)=>{
          ids += 'ids='+item.id+'&';
        });
        // 批量删除网络请求
        this.deleteRequest('/system/basic/joblevel/'+ids).then(resp=>{
          if (resp){
            // 删除成功
            // 再次刷新
            this.initJls();
          }
        })
      }).catch(()=>{
        // 点击了取消
        this.$message.info('取消操作');
      })
    },
    // 处理多选框
    handleSelectionChange(val){
        this.multipleSelection = val;

    },
    // 更新
    doUpdate(){
      this.dialogVisible = false;

      this.putRequest('/system/basic/joblevel/',this.updateJl).then(resp=>{
          if (resp){
            this.initJls();
            this.dialogVisible = false;
          }
      })
    },
    // 显示弹框
    showDialog(data){
      // 显示弹框
      Object.assign(this.updateJl,data);
      this.updateJl.createDate = '';
      this.dialogVisible = true;
    },
    // 删除职位
    deleteJoblevel(data){
      this.$confirm(`此操作将永久删除[${data.name}]是否继续？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning"
      }).then(()=>{
        // 得到职称的id
        this.deleteRequest(`/system/basic/joblevel/${data.id}`).then(resp=>{
          if (resp){
            this.initJls();
          }
        });
      }).catch(()=>{
        this.$message.info("已取消");
      })

    },
    // 添加职位
    addJoblevel(){
      if (this.jl.name && this.jl.titleLevel){
        // 具备添加的条件
        this.postRequest('/system/basic/joblevel/', this.jl).then(resp=>{
          if (resp){
            // 添加成功，初始化展示的数据
            this.initJls();
          }
        });
      }else {
        this.$message.error('字段不能为空');
      }

    },
    // 初始化
    initJls(){
      this.getRequest('/system/basic/joblevel/').then(resp=>{
        if (resp){
          this.jls = resp;
          // 将输入框和选择框中的数据清空
          this.jl.name = '';
          this.jl.titleLevel = '';
        }
      })
    }
  }
}
</script>

<style scoped>

</style>